package com.liu.netty.demo.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Date;

/**
 * @program: https://www.joinsunsoft.com
 * @description:
 * @author: LiuYong
 * @email: david.liu@ginghan.com
 * @create: 2022/3/23 10:14
 */

@Slf4j
public class ClientByteArrayDemoChannelHandler extends SimpleChannelInboundHandler<byte[]> {
    public ClientByteArrayDemoChannelHandler(String name){
        this.name = name;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        InetSocketAddress address = (InetSocketAddress) (ctx.channel().localAddress());

        ctx.writeAndFlush(ByteUtils.msg2bytes("[" + address +"]", "UTF-8"));
        ctx.writeAndFlush(ByteUtils.msg2bytes("[" + address +"]", "UTF-8"));
        ctx.writeAndFlush( ByteUtils.msg2bytes("[中文测试本地地址" + address +"]", "UTF-8"));

        super.channelActive(ctx);
    }

    private String name;
    private InetSocketAddress address;

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, byte[] msg) throws Exception {
        String txt = ByteUtils.byte2msg(msg, "UTF-8");
        log.error("================== Receive message from server: {}", txt);
    }
}
